import sys,pygame
sys.path.append('modules')
import tetris,buttons,player,keyboard
from pygame.locals import *
from costants import *
#########################################################
#########################################################
pygame.init()
screen=pygame.display.set_mode((NAME_SCREEN_WIDTH,NAME_SCREEN_HEIGHT),0,32)
icon=pygame.image.load(ICON).convert_alpha()
help0=pygame.image.load(HELP0).convert_alpha()
help1=pygame.image.load(HELP1).convert_alpha()
credits=pygame.image.load(CREDITS).convert_alpha()
pygame.display.set_icon(icon)
pygame.display.set_caption("PyTetris")
menu_bg=pygame.image.load(MENU_BG).convert()
###########################################################
############################################################
def main_menu(screen):
	player_name=keyboard.get_name(screen)
	screen=pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT),0,32)
	player_score=player.Score()
	gioc=player.Player(player_name,player_score)
	screen.blit(menu_bg,(0,0))
	new_game=buttons.Button((500,360),"NEW GAME",screen)
	help=buttons.Button((500,410),"HELP",screen)
	credits=buttons.Button((500,460),"CREDITS",screen)
	quit=buttons.Button((500,510),"EXIT",screen)
	new_game.blit()
	help.blit()
	credits.blit()
	quit.blit()
	exit=0
	while not exit:
		pygame.display.update()
		event=pygame.event.wait()
		if event.type==QUIT: exit=1
		if event.type==MOUSEBUTTONDOWN:
			if new_game.is_pressed(event.dict): 
				exit=tetris.play(screen,gioc)
				if exit==TO_MAIN:
					player_score=player.Score()
					gioc=player.Player(player_name,player_score)
				screen.blit(menu_bg,(0,0))
				new_game.blit()
				help.blit()
				credits.blit()
				quit.blit()
				pygame.display.update()
			if help.is_pressed(event.dict): 
				exit=show_help(screen)
				screen.blit(menu_bg,(0,0))
				new_game.blit()
				help.blit()
				credits.blit()
				quit.blit()
				pygame.display.update()
			if credits.is_pressed(event.dict):
				exit=show_credits(screen)
				screen.blit(menu_bg,(0,0))
				new_game.blit()
				help.blit()
				credits.blit()
				quit.blit()
				pygame.display.update()
			if quit.is_pressed(event.dict): exit=1
#############################################################
def show_help(screen):
	page=0
	back_to_main=buttons.Button((380,600),"BACK",screen)
	while 1:
		if page==0:
			change_page=buttons.Button((690,600),"NEXT PAGE",screen)
			pygame.draw.rect(screen,LIGHTYELLOW,(0,0,880,660))
			screen.blit(help0,(0,0))
			change_page.blit()
			back_to_main.blit()
			pygame.display.update()
		if page==1:
			change_page=buttons.Button((10,600),"PREVIOUS PAGE",screen)
			pygame.draw.rect(screen,LIGHTYELLOW,(0,0,880,660))
			screen.blit(help1,(0,0))
			change_page.blit()
			back_to_main.blit()
			pygame.display.update()
		event=pygame.event.wait()
		if event.type==QUIT: return EXIT_GAME
		if event.type==MOUSEBUTTONDOWN:
			if change_page.is_pressed(event.dict):
				page=(page+1)%2
			if back_to_main.is_pressed(event.dict):
				return TO_MAIN
############################################################
def show_credits(screen):
	back_to_main=buttons.Button((620,590),"BACK TO MAIN",screen)
	pygame.draw.rect(screen,LIGHTYELLOW,(0,0,880,660))
	screen.blit(credits,(0,0))
	back_to_main.blit()
	while 1:
		event=pygame.event.wait()
		if event.type==QUIT: return EXIT_GAME
		if event.type==MOUSEBUTTONDOWN:
			if back_to_main.is_pressed(event.dict): return TO_MAIN
		pygame.display.update()
###########################################################
###########################################################
main_menu(screen)
pygame.quit()
#sys.exit(0)


